package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Data_dictionary;
import cn.edu.scau.dropship.entity.Data_dictionaryExample;
import cn.edu.scau.dropship.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /*
     * 数据字典新增，管理员
     * */
    @PostMapping(value = "/add")
    @ResponseBody
    public String insert(HttpServletRequest request) {
        String type=request.getParameter("type");
        String description=request.getParameter("description");
        String code=request.getParameter("code");
        String value=request.getParameter("value");

        if(type.equals("")||description.equals("")||code.equals("")||value.equals("")){
            return "四个字段不允许为空，请重新输入";
        }
        Data_dictionary data_dictionary = new Data_dictionary();
        data_dictionary.setDictType(type);
        data_dictionary.setDictDescription(description);
        data_dictionary.setCode(code);
        data_dictionary.setCodeValue(value);

        int result = dictionaryService.insert(data_dictionary);
        if (result > 0)
            return "插入成功";
        return "插入失败";
    }

    /*
     * 数据字典删除，管理员
     * */
    @GetMapping(value = "/delete")
    @ResponseBody
    public String deleteById(HttpServletRequest request) {
        Integer id=Integer.valueOf(request.getParameter("id"));
        int result = dictionaryService.deleteByPrimaryKey(id);
        if (result > 0)
            return "删除成功";
        return "删除失败";
    }

    /*
     * 数据字典修改，管理员
     * */
    @PostMapping(value = "/update")
    @ResponseBody
    public String updateById(HttpServletRequest request) {
        //获取id
        Integer id=Integer.valueOf(request.getParameter("id"));
        Data_dictionary data_dictionary=new Data_dictionary();
        data_dictionary=dictionaryService.selectByPrimaryKey(id);

        //获取除id外值
        String type=request.getParameter("type");
        String description=request.getParameter("description");
        String code=request.getParameter("code");
        String value=request.getParameter("value");

        //检查除id外值，均不为空
        if(type.equals("")||description.equals("")||code.equals("")||value.equals("")){
            return "所有字段不允许为空值，请重新输入！";
        }

        //更新
        data_dictionary.setDictType(type);
        data_dictionary.setDictDescription(description);
        data_dictionary.setCode(code);
        data_dictionary.setCodeValue(value);
        int result = dictionaryService.updateByPrimaryKey(data_dictionary);
        if (result > 0)
            return "修改成功";
        return "修改失败";
    }

    /*@GetMapping(value = "/selectById{id}")
    public Data_dictionary selectById(@RequestParam("id") Integer id) {
        Data_dictionary data_dictionary = dictionaryService.selectByPrimaryKey(id);
        return data_dictionary;
    }*/

    /*
     * 数据字典列表条件查询，管理员
     * */
    @PostMapping(value = "/selectByType")
    @ResponseBody
    public List<Data_dictionary> selectByType(HttpServletRequest request) {
        String type=request.getParameter("type");
        Data_dictionaryExample data_dictionaryExample = new Data_dictionaryExample();
        Data_dictionaryExample.Criteria criteria = data_dictionaryExample.createCriteria();
        criteria.andDictTypeEqualTo(type);
        List<Data_dictionary> list = dictionaryService.selectByExample(data_dictionaryExample);
        return list;
    }

    //获取数据字典表列表
    @RequestMapping(value = "/getDicList",method = RequestMethod.POST)
    @ResponseBody
    public List<Data_dictionary> getDicList(HttpServletResponse response){
        List<Data_dictionary> list=dictionaryService.getlist();
        return list;
    }
}
